package com.maple.phoneward.custom;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.maple.phoneward.activity.R;

/**
 * DialogBuilder.java
 * 
 * @author YaJian Qiu
 * @time 2013-3-25 下午8:31:56<br>
 *       本类描述：用于创建对话框的工具类
 * 
 */
public class DialogBuilder {
	/**
	 * 左边按钮
	 */
	public static final int LEFT_BUTTON = 1;
	/**
	 * 右边按钮
	 */
	public static final int RIGHT_BUTTON = 2;

	private Dialog dialog;
	private Context context;

	public DialogBuilder(Context context) {
		this.context = context;

		// 初始化对话框
		Dialog dialog = new Dialog(context, R.style.MyDialog);
		dialog.setContentView(R.layout.dialog);
		this.dialog = dialog;
	}

	/**
	 * 设置对话框标题
	 */
	public DialogBuilder setTitle(Object titleText) {
		TextView titleView = getView(R.id.title);
		titleView.setText(parseParam(titleText));
		return this;
	}

	/**
	 * 设置中间显示的文字
	 */
	public DialogBuilder setMessage(Object messageText) {
		TextView messageView = getView(R.id.message);
		messageView.setText(parseParam(messageText));
		return this;
	}

	/**
	 * 设置中间要显示的View
	 */
	public DialogBuilder setView(View view) {
		// 删除中间的TextView
		LinearLayout messageLayout = getView(R.id.message_layout);
		TextView messageView = getView(R.id.message);
		messageLayout.removeView(messageView);

		// 添加新的View
		messageLayout.addView(view);
		this.centerView = view;
		return this;
	}

	private View centerView;
	/**
	 * 设置中间要显示的布局
	 */
	public DialogBuilder setView(int layoutId) {
		// 删除中间的TextView
		LinearLayout messageLayout = getView(R.id.message_layout);
		TextView messageView = getView(R.id.message);
		messageLayout.removeView(messageView);

		// 添加新的View
		// root :　实例化完布局文件后，会将布局文件的根节点添加到root中
		this.centerView = LayoutInflater.from(context).inflate(layoutId, messageLayout);
		return this;
	}
	/**
	 * 获取中间要显示的布局
	 */
	public View getCenterView() {
		// 删除中间的TextView
		return this.centerView;
	}

	Button left;

	/**
	 * 设置按钮文字和监听器
	 * 
	 * @param leftBtnText
	 *            左边按钮文字
	 * @param rightBtnText
	 *            右边按钮文字
	 */
	public DialogBuilder setButtons(Object leftBtnText, Object rightBtnText, final DialogInterface.OnClickListener listener) {
		// 设置左边按钮文字
		Button left = getView(R.id.left);
		left.setText(parseParam(leftBtnText));
		left.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 先关闭对话框
				dialog.dismiss();

				if (listener != null) {
					listener.onClick(dialog, LEFT_BUTTON);
				}
				// listener.onDialogButtonClick(dialog, 0);
			}
		});
		this.left = left;

		// 设置右边按钮文字
		Button right = getView(R.id.right);
		right.setText(parseParam(rightBtnText));
		right.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 先关闭对话框
				dialog.dismiss();
				if (listener != null) {
					listener.onClick(dialog, RIGHT_BUTTON);
				}
				// listener.onDialogButtonClick(dialog, 1);
			}
		});
		return this;
	}
	public DialogBuilder setOnClickListener(final DialogInterface.OnClickListener listener) {
		// 设置左边按钮文字
		Button left = getView(R.id.left);
		left.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 先关闭对话框
				dialog.dismiss();
				
				if (listener != null) {
					listener.onClick(dialog, LEFT_BUTTON);
				}
				// listener.onDialogButtonClick(dialog, 0);
			}
		});
		this.left = left;
		
		// 设置右边按钮文字
		Button right = getView(R.id.right);
		right.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 先关闭对话框
				dialog.dismiss();
				
				if (listener != null) {
					listener.onClick(dialog, RIGHT_BUTTON);
				}
				// listener.onDialogButtonClick(dialog, 1);
			}
		});
		return this;
	}

	/**
	 * 根据控件id取得对应的控件
	 * 
	 * @param resId
	 *            控件id
	 */
	@SuppressWarnings("unchecked")
	public <T extends View> T getView(int resId) {
		return (T) dialog.findViewById(resId);
	}

	/**
	 * 返回一个对话框
	 */
	public Dialog create() {
		if (left == null) { // 说明不需要按钮，移除按钮所在的布局
			LinearLayout root = getView(R.id.root);
			LinearLayout btnsLayout = getView(R.id.btns_layout);
			root.removeView(btnsLayout);
		}
		return dialog;
	}

	/**
	 * 解析参数
	 */
	private String parseParam(Object param) {
		if (param instanceof Integer) {
			return context.getString((Integer) param);
		} else if (param instanceof String) {
			return param.toString();
		}
		return null;
	}

	public interface OnDialogButtonClickListener {
		public void onDialogButtonClick(Dialog dialog, int position);
	}
	
	
}
